﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using VMuktiService;
using System.ServiceModel;
using Chat.Business.Service.DataContracts;
using VMuktiAPI;

namespace Chat.Business.Service.BasicHttp
{
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
    public class clsHttpChat : IHttpChat
    {
        public delegate void delsvcJoin(string uName);
        public delegate void delsvcSendMessage(string msg, string from, List<string> to);
        public delegate List<clsMessage> delsvcGetMessages(string recipient);
        public delegate void delsvcSetUserList(string uname);
        public delegate void delsvcGetUserList(string uname);
        public delegate void delsvcSignOutChat(string from, List<string> to);
        public delegate void delsvcUnJoin(string uName);

        public event delsvcJoin EntsvcJoin;
        public event delsvcSendMessage EntsvcSendMessage;
        public event delsvcGetMessages EntsvcGetMessages;
        public event delsvcSetUserList EntsvcSetUserList;
        public event delsvcGetUserList EntsvcGetUserList;
        public event delsvcSignOutChat EntsvcSignOutChat;
        public event delsvcUnJoin EntsvcUnJoin;

        #region IHttpChat Members

        public void svcJoin(string uname)
		{
			try
			{
				if (EntsvcJoin != null)
				{
					EntsvcJoin(uname);
				}
			}
			catch (Exception exp)
			{
				exp.Data.Add("My Key", "svcJoin()--:--clsHttpChat.cs--:--" + exp.Message + " :--:--");
				ClsException.LogError(exp);
				ClsException.WriteToErrorLogFile(exp);
			}
        }

        public void svcSendMessage(string msg, string from, List<string> to)
		{
			try
			{
				if (EntsvcSendMessage != null)
				{
					EntsvcSendMessage(msg, from, to);
				}
			}
			catch (Exception exp)
			{
				exp.Data.Add("My Key", "svcSendMessage()--:--clsHttpChat.cs--:--" + exp.Message + " :--:--");
				ClsException.LogError(exp);
				ClsException.WriteToErrorLogFile(exp);
			}
        }

        public List<clsMessage> svcGetMessages(string recipient)
		{
			try
			{

				if (EntsvcGetMessages != null)
				{
					return EntsvcGetMessages(recipient);
				}
				else
				{
					return null;
				}
			}
			catch (Exception exp)
			{
				exp.Data.Add("My Key", "svcGetMessages()--:--clsHttpChat.cs--:--" + exp.Message + " :--:--");
				ClsException.LogError(exp);
				ClsException.WriteToErrorLogFile(exp);
				return null;
			}
        }

        public void svcSetUserList(string uname)
		{
			try
			{
				if (EntsvcSetUserList != null)
				{
					EntsvcSetUserList(uname);
				}
			}
			catch (Exception exp)
			{
				exp.Data.Add("My Key", "svcSetUserList()--:--clsHttpChat.cs--:--" + exp.Message + " :--:--");
				ClsException.LogError(exp);
				ClsException.WriteToErrorLogFile(exp);
			}
        }

        public void svcGetUserList(string uname)
		{
			try
			{
				if (EntsvcGetUserList != null)
				{                   
					EntsvcGetUserList(uname);
				}
			}
			catch (Exception exp)
			{
				exp.Data.Add("My Key", "svcGetUserList()--:--clsHttpChat.cs--:--" + exp.Message + " :--:--");
				ClsException.LogError(exp);
				ClsException.WriteToErrorLogFile(exp);
			}
        }

        public void svcSignOutChat(string from, List<string> to)
		{
			try
			{
				if (EntsvcSignOutChat != null)
				{
					EntsvcSignOutChat(from, to);
				}
			}
			catch (Exception exp)
			{
				exp.Data.Add("My Key", "svcSignOutChat()--:--clsHttpChat.cs--:--" + exp.Message + " :--:--");
				ClsException.LogError(exp);
				ClsException.WriteToErrorLogFile(exp);
			}
        }
        
        public void svcUnJoin(string uname)
		{
			try
			{
				if (EntsvcUnJoin != null)
				{
					EntsvcUnJoin(uname);
				}
			}
			catch (Exception exp)
			{
				exp.Data.Add("My Key", "svcUnJoin()--:--clsHttpChat.cs--:--" + exp.Message + " :--:--");
				ClsException.LogError(exp);
				ClsException.WriteToErrorLogFile(exp);
			}
        }

        #endregion
    }
}
